package com.person.webdemoproject2.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.person.webdemoproject2.mapper.FindMapper;
import com.person.webdemoproject2.pojo.*;
import com.person.webdemoproject2.utils.jwtUtils;
import io.jsonwebtoken.Claims;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@Service
public class FindServiceimpl implements FindService {
    @Autowired
    FindMapper findMapper;


    @Override
    public User userinfo(String token) {
        String jwt = token;
        jwtUtils.parseJWT(jwt);
        Claims claims = jwtUtils.parseJWT(jwt);
        Integer  userid = (Integer) claims.get("id");

        return findMapper.userinfo(userid);
    }

    @Override
    public Integer countf(String token) {
        String jwt = token;
        jwtUtils.parseJWT(jwt);
        Claims claims = jwtUtils.parseJWT(jwt);
        Integer  userid = (Integer) claims.get("id");

        return findMapper.countf(userid);
    }

    @Override
    public Integer fans(String token) {
        String jwt = token;
        jwtUtils.parseJWT(jwt);
        Claims claims = jwtUtils.parseJWT(jwt);
        Integer  userid = (Integer) claims.get("id");

        return findMapper.fans(userid);
    }

    @Override
    public User byusername(String username) {
        return findMapper.byusername(username);
    }

    @Override
    public List<UserRelationship> fname(String token) {
        String jwt = token;
        jwtUtils.parseJWT(jwt);
        Claims claims = jwtUtils.parseJWT(jwt);
        Integer  userid = (Integer) claims.get("id");
        return findMapper.fname(userid);
    }

    @Override
    public List<Post> getPostsByDate(LocalDate begin, LocalDate end,Integer page,Integer pageSize) {
        PageHelper.startPage(page, pageSize);

        return findMapper.getPostsByDate(begin,end);
    }

    @Override
    public List<Post> getPostsByLikes(Integer page,Integer pageSize) {
        PageHelper.startPage(page, pageSize);
        return findMapper.getPostsByLikes();
    }

    @Override
    public List<Post> getPostsBykeyword(String keyword) {
        return findMapper.getPostsBykeyword(keyword);
    }

    @Override
    public List<Post> collections(String token) {
        String jwt = token;
        jwtUtils.parseJWT(jwt);
        Claims claims = jwtUtils.parseJWT(jwt);
        Integer  userid = (Integer) claims.get("id");

        List<Integer> postId=findMapper.getPostid(userid);
        List<Post> Posts= new ArrayList<>();
        for (Integer postid:postId) {
            Posts.add(findMapper.Conllections(postid));
        }
        return Posts;
    }

    @Override
    public List<Comment> comments(Integer postId) {
        List<Comment> comments=findMapper.comments(postId);
        return comments;
    }

    @Override
    public List<reComment>recomments(Integer commentId, int page, int pageSize) {
        PageHelper.startPage(page, pageSize);
        return findMapper.recomments(commentId);
    }

    @Override
    public List<Post> getPostsByfans(String token, int page, int pageSize) {
        Integer userId = jwtUtils.getUserId(token);

        List<Integer> byuserid = findMapper.byuserid(userId);

        List<Post> Posts= new ArrayList<>();
        for (Integer userid:byuserid) {
            for (Post post:findMapper.GetPostByuserid(userid)) {
                Posts.add(post);
            }
        }
        PageHelper.startPage(page, pageSize);
        return Posts;
    }

    @Override
    public List<Post> getPostsByuserid(String token, int page, int pageSize) {
        Integer userId = jwtUtils.getUserId(token);
        PageHelper.startPage(page, pageSize);
       return findMapper.GetPostByuserid(userId);



    }

    @Override
    public List<Comment> getCommentByuserid(String token, int page, int pageSize) {
        Integer userId = jwtUtils.getUserId(token);
        PageHelper.startPage(page, pageSize);
        return findMapper.GetCommentByuserid(userId);
    }

    @Override
    public List<Post> GetPostsByuserid(Integer userid, int page, int pageSize) {
        PageHelper.startPage(page, pageSize);
        return findMapper.GetPostByuserid(userid);
    }

    @Override
    public Post getPostById(Integer postId) {
        return findMapper.getPostById(postId);
    }

    @Override
    public User getUserByID(Integer userId) {
        return findMapper.userinfo(userId);
    }


}
